package com.wandian.ui.controls
{	
	import flash.display.*;
	import flash.events.*;
	
	import utils.FrameChecker;
	import utils.ObjectTools;
	
	public class Animation extends MovieClip
	{
		private var fAnimationShell_mc:MovieClip;
		private var fAllowAnimation_bl:Boolean;
		private var fStartInProgress_bl:Boolean;
		private var fStopInProgress_bl:Boolean;
		private var fSuspended_bl:Boolean;
		private var fCompleteInProgress_bl:Boolean;
		private var fFrameEnterController_fch:FrameChecker;
		public static const EVENT_ANIMATION_STARTED:String = "onAnimationStarted";
		private static const LABEL_COMPLETE:String = "complete_lb";
		public static const EVENT_ANIMATION_COMPLETED:String = "onAnimationCompleted";
		public static const EVENT_ANIMATION_STOPPED:String = "onAnimationStopped";
		private static const LABEL_STOP:String = "stop_lb";
		private static const LABEL_ANIMATION:String = "animation_lb";
		
		public function Animation(param1:MovieClip = null)
		{
			this.fFrameEnterController_fch = new FrameChecker();
			if (param1)
			{
				this.animationShell = param1;
			}
			else
			{
				this.animationShell = this;
			}
			this.animationShell.gotoAndStop(LABEL_STOP);
			this.allowAnimation = true;
			this.updateState(false, false, false, false);
			return;
		}// end function
		
		public function get animationShell() : MovieClip
		{
			return this.fAnimationShell_mc;
		}// end function
		
		public function get stopInProgress() : Boolean
		{
			return this.fStopInProgress_bl;
		}// end function
		
		public function get animationInProgress() : Boolean
		{
			return !this.animationStopped && !this.stopInProgress && !this.animationCompleted;
		}// end function
		
		protected function get nativeAnimation() : MovieClip
		{
			if (this.nativeAnimationContent)
			{
				return this.nativeAnimationContent.content;
			}
			return MovieClip(this._getAsset(this.animationShell, "animation_mc", false));
		}// end function
		
		public function get animationCompleted() : Boolean
		{
			return this.animationShell.currentLabel == LABEL_COMPLETE && !this.completeInProgress;
		}// end function
		
		public function set animationShell(param1:MovieClip) : void
		{
			if (this.animationShell != param1)
			{
				this.detachAnimation();
				this.fAnimationShell_mc = param1;
				if (this.animationShell && this.animationShell != this)
				{
					this.addChild(this.animationShell);
				}
			}
			return;
		}// end function
		
		protected function detachAnimation() : void
		{
			if (!this.animationShell)
			{
				return;
			}
			this.animationShell.removeEventListener(EVENT_ANIMATION_COMPLETED, this.onNativeAnimationCompleted);
			this.animationShell.removeEventListener(EVENT_ANIMATION_STARTED, this.onAnimationStarted);
			this.fFrameEnterController_fch.reset();
			try
			{
				this.removeChild(this.animationShell);
			}
			catch (aError:Error)
			{
			}
			return;
		}// end function
		
		public function get allowAnimation() : Boolean
		{
			return this.fAllowAnimation_bl;
		}// end function
		
		protected function updateState(param1:Boolean, param2:Boolean, param3:Boolean, param4:Boolean) : void
		{
			this.fStartInProgress_bl = param1;
			this.fCompleteInProgress_bl = param2;
			this.fStopInProgress_bl = param3;
			this.fSuspended_bl = param4;
			return;
		}// end function
		
		protected function simulateAnimationPlaying() : void
		{
			if (this.animationShell.currentLabel != LABEL_STOP)
			{
				this.fFrameEnterController_fch.reset();
				this.fFrameEnterController_fch.checkLabel(this.animationShell, LABEL_STOP, this.simulateAnimationPlaying);
				this.animationShell.gotoAndStop(LABEL_STOP);
				return;
			}
			this.onAnimationStarted();
			this.onAnimationCompleted();
			return;
		}// end function
		
		protected function onAnimationStarted(event:Event = null) : void
		{
			this.fStartInProgress_bl = false;
			if (event && event.target)
			{
				event.target.removeEventListener(EVENT_ANIMATION_STARTED, this.onAnimationStarted);
			}
			if (this.animationShell != this || !event)
			{
				this.dispatchEvent(new Event(EVENT_ANIMATION_STARTED));
			}
			if (this.nativeAnimationContent)
			{
			}
			if (this.allowAnimation)
			{
				this.nativeAnimation.addEventListener(EVENT_ANIMATION_COMPLETED, this.onNativeAnimationCompleted);
				this.nativeAnimation.gotoAndPlay(1);
			}
			return;
		}// end function
		
		protected function onAnimationCompleted() : void
		{
			this.updateState(false, false, false, false);
			this.dispatchEvent(new Event(EVENT_ANIMATION_COMPLETED));
			return;
		}// end function
		
		public function set allowAnimation(param1:Boolean) : void
		{
			this.fAllowAnimation_bl = param1;
			if (!this.allowAnimation && !this.animationStopped)
			{
				this.stopToPlayAnimation();
			}
			return;
		}// end function
		
		public function get startInProgress() : Boolean
		{
			return this.fStartInProgress_bl;
		}// end function
		
		public function startToPlayAnimation() : void
		{
			this.frameEnterController.reset();
			if (this.allowAnimation)
			{
				this.updateState(true, false, false, false);
				this.animationShell.addEventListener(EVENT_ANIMATION_STARTED, this.onAnimationStarted);
				if (this.animationShell.currentLabel == LABEL_ANIMATION && this.nativeAnimation)
				{
					trace("Animation.startToPlayAnimation(): LABEL_ANIMATION");
					this.animationShell.gotoAndStop(LABEL_ANIMATION);
					this.animationShell.dispatchEvent(new Event(EVENT_ANIMATION_STARTED));
				}
				else
				{
					this.animationShell.gotoAndStop(LABEL_ANIMATION);
				}
			}
			else
			{
				this.simulateAnimationPlaying();
			}
			return;
		}// end function
		
		public function get animationSuspendable() : Boolean
		{
			return this.animationInProgress && !this.completeInProgress && !this.stopInProgress;
		}// end function
		
		private function _getAsset(param1:Object, param2:String, param3:Boolean) : Object
		{
			return ObjectTools.getAssetByName(param1, param2, param3);
		}// end function
		
		public function get completeInProgress() : Boolean
		{
			return this.fCompleteInProgress_bl;
		}// end function
		
		protected function onNativeAnimationCompleted(event:Event = null) : void
		{
			if (event)
			{
				MovieClip(event.target).removeEventListener(EVENT_ANIMATION_COMPLETED, this.onNativeAnimationCompleted);
			}
			this.nativeAnimation.stop();
			this.fFrameEnterController_fch.reset();
			this.fFrameEnterController_fch.checkLabel(this.animationShell, LABEL_COMPLETE, this.onAnimationCompleted);
			this.updateState(false, true, false, this.animationSuspended);
			this.animationShell.gotoAndStop(LABEL_COMPLETE);
			return;
		}// end function
		
		public function suspendAnimation() : void
		{
			if (this.animationSuspendable)
			{
				this.updateState(this.startInProgress, this.completeInProgress, this.stopInProgress, true);
				this.nativeAnimation.stop();
			}
			return;
		}// end function
		
		public function get animationStopped() : Boolean
		{
			return this.animationShell.currentLabel == LABEL_STOP && !this.startInProgress && !this.stopInProgress;
		}// end function
		
		protected function get frameEnterController() : FrameChecker
		{
			if (!this.fFrameEnterController_fch)
			{
				this.fFrameEnterController_fch = new FrameChecker();
			}
			return this.fFrameEnterController_fch;
		}// end function
		
		protected function onAnimationStopped() : void
		{
			trace("Animation.onAnimationStopped() called");
			this.updateState(false, false, false, false);
			this.dispatchEvent(new Event(EVENT_ANIMATION_STOPPED));
			return;
		}// end function
		
		public function stopToPlayAnimation() : void
		{
			if (!this.animationStopped)
			{
				this.updateState(false, false, true, false);
				this.animationShell.removeEventListener(EVENT_ANIMATION_STARTED, this.onAnimationStarted);
				if (this.nativeAnimation)
				{
					this.nativeAnimation.removeEventListener(EVENT_ANIMATION_COMPLETED, this.onAnimationCompleted);
					if (this.nativeAnimation.currentFrame != 1)
					{
						this.fFrameEnterController_fch.reset();
						this.fFrameEnterController_fch.checkFrame(this.nativeAnimation, 1, this.stopToPlayAnimation);
						this.nativeAnimation.gotoAndStop(1);
						return;
					}
				}
				this.fFrameEnterController_fch.reset();
				this.fFrameEnterController_fch.checkLabel(this.animationShell, LABEL_STOP, this.onAnimationStopped);
				this.animationShell.gotoAndStop(LABEL_STOP);
			}
			return;
		}// end function
		
		public function get animationSuspended() : Boolean
		{
			return this.fSuspended_bl;
		}// end function
		
		protected function get nativeAnimationContent() : ContentController
		{
			try
			{
			}
			catch (aError_err:Error)
			{
				return null;
			}
			return ContentController(this.animationShell["animation_mc"]);
		}// end function
		
		public function resumeAnimation() : void
		{
			if (this.animationSuspended)
			{
				this.updateState(this.startInProgress, this.completeInProgress, this.stopInProgress, false);
				this.nativeAnimation.play();
			}
			return;
		}// end function
		
	}
}
